var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    allPrice:0,  //全部价格
    price:0,
    showView: false, // 编辑状态
    selectNum: 0,
    selectAllStatus: false ,   // 全选状态
    recids:'',
    deleteGoods:'',
    deleteSelect:'',
    goodsId:'',
    goodsName:'',
    goodsPrice:'',
    goodsNumber:'',
    PushList:'',
    pageSize:100,
    pageIndex:1,
  }, 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    showView: (options.showView == "true" ? true : false)
    if (options.allPrice == undefined) {
      var that = this;
      this.setData({
        allPrice: that.data.price
      })
    }
    this.shoppingItem()
  },
  onShow:function(){
    this.shoppingItem();
  },
  /**
   * 商品列表
   */
  shoppingItem(){
    var that = this;
    const userid = app.globalData.userid
    wx.request({
      url: app.globalData.url+'getShopCart',
      data: {
        cartType:2,
        userId: userid,
        pageSize:that.data.pageSize,
        pageIndex:that.data.pageIndex
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        that.setData({
          goodsList: res.data.object.cartList,
        })
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  jian:function(e){
    var that = this
    const index = e.target.dataset.index;
    const numbe = e.target.dataset.numb;
    const pricer = e.target.dataset.goodpricer;
    const recId = e.target.dataset.recid;
    const goods = this.data.goodsList[index];
    
    if (numbe >= 2){
      console.log(goods)
      goods.goodsNumber--;
      this.getAllPrice();
      that.setData({
        goodsList: that.data.goodsList,
        allPrice: this.data.allPrice
      }) 
      wx.request({
        url: app.globalData.url+'updateGoodsNumberInCart',
        data: {
          recId: recId,
          goodsNumber: goods.goodsNumber--
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        method: 'GET',
        success: function(res) {
          that.shoppingItem()
        },
        fail: function(res) {},
        complete: function(res) {},
      })
    }else if (numbe <= 1){
        wx.showToast({
          title: '宝贝不能再减少了~',
          icon:'none',
          duration: 1000,
          mask: true,
          success: function(res) {
            that.setData({
              goodsList: that.data.goodsList
            })
          },
          fail: function(res) {},
          complete: function(res) {},
        })
    }
  },
  jia:function(e){
    var that = this
    const index = e.target.dataset.index;
    const goods = this.data.goodsList[index];
    const recId = e.target.dataset.recid;
    goods.goodsNumber++;
    this.getAllPrice();    
    this.setData({
      allPrice: this.data.allPrice,
      goodsList: this.data.goodsList,
    })
    wx.request({
      url: app.globalData.url+'updateGoodsNumberInCart',
      data: {
        recId: recId,
        goodsNumber: goods.goodsNumber++
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      method: 'GET',
      success: function (res) {
        that.shoppingItem()
      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  editor:function(){
    this.getAllPrice();
    this.setData({
      showView: !this.data.showView,
      allPrice: this.data.allPrice
    })
  },
  /**
   * 全选
   */
  selectAll(e) {
    if (this.data.selectAllStatus){
      for(const goods of this.data.goodsList){
        goods.select = false;
      }
      this.data.selectNum = 0;
      this.getAllPrice();      
      this.setData({
        selectAllStatus: false,
        goodsList: this.data.goodsList,
        allPrice: this.data.allPrice
      })
      
    }else{
      for (const goods of this.data.goodsList) {
        goods.select = true;
      }
      //全选结算
      let goodsId = []
      for (let i = 0; i < this.data.goodsList.length; i++) {
        if (this.data.goodsList[i].select) {
          goodsId.push(this.data.goodsList[i])
        }
      }
      this.data.PushList = goodsId 

      this.data.selectNum = this.data.goodsList.length;
      this.getAllPrice();
      this.setData({
        selectAllStatus: true,
        goodsList: this.data.goodsList,
        allPrice: this.data.allPrice
      })
    }
  },
  /**
   * 选择商品
   */
  selectList(e){
    const index = e.target.dataset.index;
    this.deteleIndex = e.target.dataset.index;
    this.data.recids = this.data.goodsList[index].recId
    this.data.goodsList[index].select = !this.data.goodsList[index].select;
    this.data.select = this.data.goodsList[index].select
    this.data.goodsId = this.data.goodsList[index].goodsId
    this.data.goodsName = this.data.goodsList[index].goodsName
    this.data.goodsPrice = this.data.goodsList[index].goodsPrice
    this.data.goodsNumber = this.data.goodsList[index].goodsNumber

    //单选结算
    let goodsId = []
    for (let i = 0; i < this.data.goodsList.length; i++) {
      if (this.data.goodsList[i].select) {
        goodsId.push(this.data.goodsList[i])
      }
    }
    this.data.PushList = goodsId 

    if (this.data.goodsList[index].select){
      this.data.selectNum++;
    }else{
      this.data.selectNum--;
    }
    this.getAllPrice();
    if(this.data.selectNum === this.data.goodsList.length){
      this.setData({
        goodsList: this.data.goodsList,
        selectAllStatus: true,
        allPrice: this.data.allPrice
      })
    }else{
      this.setData({
        goodsList: this.data.goodsList,
        selectAllStatus: false,
        allPrice: this.data.allPrice
      })
    }
  },
  /**
   * 获取全部价格
   */
  getAllPrice(){
    this.data.allPrice = 0;
    console.log(this.data.allPrice)
    for(const goods of this.data.goodsList){
      if(goods.select){
        this.data.allPrice += goods.goodsPrice * goods.goodsNumber;
      }
    }
    this.data.allPrice = this.data.allPrice.toFixed(2)
  },
  /**
   * 删除
   */
  del:function(e){
    var that = this;
    var Ids = []
    for (let i = 0; i < this.data.goodsList.length; i++) {
      if (this.data.goodsList[i].select) {
        Ids.push(this.data.goodsList[i].recId)
      }
    }
    that.setData({
      goodsList: that.data.goodsList.filter(goods => {
        if (!goods.select) return true;
        return !goods.select
      })
    })   
  
    wx.request({
      url: app.globalData.url +'deleteGoodsInCartByRecId',
      data: {
        list:Ids
      },
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' 
      }, success: function(res) {},
    })
  },
  /**
   * 结算按钮
   */
  payAll:function(e){
    this.getAllPrice();
    const allprices = this.data.allPrice
    if (allprices == 0.00){
      wx.showToast({
        title: '请选择需要购买的商品',
        icon: 'none',
        duration: 1000,
        mask: true,
        success: function(res) {},
        fail: function(res) {},
        complete: function(res) {},
      })
    }else{
      const list = JSON.stringify(this.data.PushList)
      app.globalData.goodsList = list
      wx.navigateTo({
        url: '../orderPay/orderPay' ,
        success: function (res) { },
        fail: function (res) { },
        complete: function (res) { },
      })
    }
  },
  /**
   * 点击事件
   */
  commodityDetail:function(e){
    const goodId = e.currentTarget.dataset.goodsid
    console.log(goodId)
    wx.navigateTo({
      url: '../commodityDetail/commodityDetail?goodsId='+goodId,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  }
})